package cn.itrigger.utils;

import cn.itrigger.config.VersionConfig;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.util.Date;
import java.util.List;

public class GitOperatorUtils {

    public static void setUserInfo(String userName, String userEmail) {
        String command = "git config --local ";

        String userNameCommand = command + "user.name \"" + userName +"\"";
        System.out.println(userNameCommand);
        CommandLineUtils.execCmdWithoutResult(userNameCommand);
        String userEmailCommand = command + "user.email \"" + userEmail + "\"";
        System.out.println(userEmailCommand);
        CommandLineUtils.execCmdWithoutResult(userEmailCommand);
    }

    /**
     * 提交修改的pom.xml文件
     * @param files
     */
    public static void commit(List<String> files) {
        StringBuffer command = new StringBuffer("git commit ");
        for (String file : files) {
            command.append(file).append(" ");
        }
        command.append("-m \"版本管理修改pom.xml文件")
                .append(DateFormatUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss")).append("\"");
        System.out.println(command.toString());
        String result = CommandLineUtils.execCmdWithResult(command.toString());
        System.out.println(result);
    }

    public static void push(String message) {
        StringBuffer sb = new StringBuffer("git push ");
        sb.append(message).append(" -f");
        String result = CommandLineUtils.execCmdWithResult(sb.toString());
        System.out.println(result);
    }

    /**
     * 列出未提交的文件
     * @return
     */
    public static String[] diff() {
        String command = "git diff --name-only";
        String result = CommandLineUtils.execCmdWithResult(command);
        if (null == result || result.trim().length() == 0) {
            return null;
        }
        return result.trim().split(System.lineSeparator());
    }

    /**
     * 切换分支
     * @param branch
     */
    public static void checkout(String branch) {
        StringBuffer sb = new StringBuffer("git checkout ");
        sb.append(branch);
        String result = CommandLineUtils.execCmdWithResult(sb.toString());
        System.out.println(result);
    }

    /**
     * 根据当前分支打标签
     * @param tagName
     */
    public static void tag(String tagName) {
        StringBuffer sb = new StringBuffer("git tag ");
        sb.append(tagName);
        String tagResult = CommandLineUtils.execCmdWithResult(sb.toString());
        System.out.println(tagResult);
        //将tag推送到git服务器
        push(" origin " + tagName);
    }

    /**
     * git配置
     * @param config
     */
    public static void config(VersionConfig config) {
//        String common = "git config --local ";
        //git地址
//        String gitUrl = config.getGitUrl();

//        String remoteOriginUrl = "http://" + config.getName() + ":" + config.getPassword() + "@" + StringUtils.substringAfter(gitUrl,"http://");

//        System.out.println(new StringBuffer(common).append("remote.origin.url \"").append(remoteOriginUrl).append("\"").toString());
//        CommandLineUtils.execCmdWithoutResult(new StringBuffer(common).append("remote.origin.url \"").append(remoteOriginUrl).append("\"").toString());



        String showConfig = "git config --list --local";
        String result = CommandLineUtils.execCmdWithResult(showConfig);

        System.out.println(result);
    }

    public static void pull(String branch) {
        String commond = "git pull origin " + branch;

        String result = CommandLineUtils.execCmdWithResult(commond);
        System.out.println(result);
    }

    public static void clean() {
        String commond = "git clean -df";
        System.out.println(commond);
        String result = CommandLineUtils.execCmdWithResult(commond);
        System.out.println(result);
    }

    public static void reset() {
        String commond = "git reset --hard";
        System.out.println(commond);
        String result = CommandLineUtils.execCmdWithResult(commond);
        System.out.println(result);
    }
}
